package com.example.keli.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.text.TextUtils;

import com.example.keli.base.provider.MediaLoader;
import com.example.keli.base.utils.LogUtils;
import com.example.keli.base.utils.SPUtils;
import com.example.keli.base.widget.glide.BGAGlideImageLoader3;
import com.example.keli.ui.register.LoginData;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pgyersdk.crash.PgyCrashManager;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumConfig;

import java.util.Locale;

import cn.bingoogolapple.photopicker.imageloader.BGAImage;


/**
 * Created by jerry on 2018/12/19.
 */

public class App extends BaseApplication {

    @SuppressLint("StaticFieldLeak")
    private static Context mContext;
    public Typeface typeface;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;

        //注册crashHandler
        PgyCrashManager.register();
        LogUtils.logInit(AppConfig.LOG_DEBUG);

        //解决Glide依赖冲突
        BGAImage.setImageLoader(new BGAGlideImageLoader3());

        Album.initialize(AlbumConfig.newBuilder(this)
                .setAlbumLoader(new MediaLoader())
                .setLocale(Locale.getDefault())
                .build());

        UMConfigure.init(this,"5ca17f0761f5641a3e0005f4"
                ,"Umeng",UMConfigure.DEVICE_TYPE_PHONE,"");
    }

    {
        PlatformConfig.setWeixin("wx17f2e7739f1c313c", "0ebfc856aab6c6e548c5e433e9ee7cd9");
    }


    public static Context getContext() {
        return mContext;
    }

    private static LoginData sAccountInfo;

    /**
     * 解析当前登录的用户
     *
     * @return AccountInfo
     */
    public static LoginData parseAccount() {
        String accountString = (String) SPUtils.get(mContext, AppConfig.PERSONAL_DATA, "");
        if (!TextUtils.isEmpty(accountString)) {
            sAccountInfo = new Gson().fromJson(accountString, new TypeToken<LoginData>() {
            }.getType());
            return sAccountInfo;
        }
        return new LoginData();
    }

    /**
     * 得到当前登录用户
     */
    public static LoginData getAccount() {
        if (sAccountInfo == null) {
            return parseAccount();
        }
        return sAccountInfo;
    }

    /**
     * 设置当前登录用户
     */
    public static void setAccount(LoginData accountInfo) {
        sAccountInfo = accountInfo;
    }

    public static boolean isLogin() {
        return sAccountInfo != null;
    }

    public static String getUid() {
        return getAccount().getUid();
    }

    public static String getToken() {
        return getAccount().getToken();
    }
}
